package com.jservice.web.smkBanner;

import com.jservice.framework.dto.Order;
import com.jservice.framework.dto.Pageable;
import com.jservice.framework.dto.Pager;
import com.jservice.framework.idWorker.Sequence;
import com.jservice.framework.spring.SpringContextUtil;
import com.jservice.model.SmkBanner;
import com.jservice.web.base.JBaseService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * SmkBannerService
 *
 * @Author cwledit
 * @DATE 2017/6/12.
 */
@Service("smkBannerService")
@Transactional(readOnly = true)
public class SmkBannerService extends JBaseService {

    public static final SmkBanner DAO = new SmkBanner();

    public static SmkBannerService me() {
        return SpringContextUtil.getBean("smkBannerService");
    }

    @Resource
    private Sequence idWorker;

    public Pager findPage(Pageable pageable) {
        List<Order> orders = new ArrayList<Order>();
        Order order1 = new Order();
        order1.setProperty("orders");
        order1.setDirection(Order.Direction.desc);
        orders.add(order1);
        pageable.setOrders(orders);
        return findPage(pageable,DAO, DAO.TABLE_NAME);
    }

    public SmkBanner findById(String bannerId){
        return DAO.findById(bannerId);
    }

    @Transactional(readOnly = false)
    public boolean save(SmkBanner smkBanner) {

        smkBanner.setId(idWorker.nextIdStr());
        smkBanner.setCreateDate(new Date());
        smkBanner.setModifyDate(new Date());
        if (smkBanner.getOrders() == null){
            smkBanner.setOrders(0);
        }

        return smkBanner.save();
    }

    @Transactional(readOnly = false)
    public boolean update(SmkBanner smkBanner) {

        smkBanner.setModifyDate(new Date());
        if (smkBanner.getOrders() == null){
            smkBanner.setOrders(0);
        }
        return smkBanner.update();
    }

}
